<template>
  <div class="gl_contentBox gl_heightFull">
    <div class="gl_mb_15">
      <el-button type="primary" size="mini" @click="addGrade()">新增认证等级</el-button>
    </div>
    <el-table ref="gradeTable" v-loading="loading" :data="gradeTable" size="mini">
      <el-table-column type="index" width="55" />
      <el-table-column prop="name" label="认证等级" />
      <el-table-column prop="competitionLevel.name" label="行政级别" />
      <el-table-column label="操作" width="300">
        <template slot-scope="{row}">
          <el-button size="mini" type="text" @click="edit(row)">编辑</el-button>
          <el-button v-if="row.isLocked" size="mini" type="text" @click="locking(row)">
            <span class="gl_color_success">解锁</span>
          </el-button>
          <el-button v-else size="mini" type="text" @click="locking(row)">
            <span class="gl_color_warning">锁定</span>
          </el-button>
          <el-button size="mini" type="text" @click="remove(row)">
            <span class="gl_color_error">删除</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row class="gl_pt_20" style="text-align: center;">
      <el-pagination
        :current-page="Api_GET_EventsManagementCompetitionCertificationLevelQO.Page"
        :page-size="Api_GET_EventsManagementCompetitionCertificationLevelQO.MaxResultCount"
        :total="totalPage"
        layout="total,sizes,prev,pager,next,jumper"
        :page-sizes="[10, 20, 50, 100]"
        @size-change="sizeChange"
        @current-change="pageIndexChange"
      />
    </el-row>
    <AddGrade ref="addGrade" :form-data="formData" :title="title" :row-id="rowId" />
  </div>
</template>

<script>
import AddGrade from './addGrade';
import { CopyData } from '@/lib';
import { mapActions } from 'vuex';
import {
  Api_GET_EventsManagementCompetitionCertificationLevelQO, // 获取认证等级列表
  SiSTKWEventsManagementCreateCompetitionCertificationLevelInput, // 新增认证等级
  SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput // 修改认证等级
} from '@/lib/form/game/swaggerForm';
import {
  api_GET_EventsManagementCompetitionCertificationLevel, // 获取认证等级列表
  api_PUT_EventsManagementCompetitionCertificationLevelId, // 修改认证等级
  api_DELETE_EventsManagementCompetitionCertificationLevelId // 删除认证等级
} from '@/extend/api/gameswaggerApi/app.js';
export default {
  name: 'GameLevel',
  components: {
    AddGrade
  },
  data () {
    return {
      loading: false,
      Api_GET_EventsManagementCompetitionCertificationLevelQO: new Api_GET_EventsManagementCompetitionCertificationLevelQO(),
      SiSTKWEventsManagementCreateCompetitionCertificationLevelInput: new SiSTKWEventsManagementCreateCompetitionCertificationLevelInput(),
      SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput: new SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput(),
      formData: {},
      totalPage: 0,
      rowId: '',
      title: '',
      gradeTable: []
    };
  },
  created () {
    this.getData();
  },
  methods: {
    ...mapActions({ getCompetitionCertificationLevel: 'dict/getCompetitionCertificationLevel' }),
    // 获取表格数据
    getData(page) {
      var that = this;
      that.loading = true;
      that.Api_GET_EventsManagementCompetitionCertificationLevelQO.IncludeCompetitionLevel = true;
      that.Api_GET_EventsManagementCompetitionCertificationLevelQO.IsLocked = null;
      that.Api_GET_EventsManagementCompetitionCertificationLevelQO.Page = page || 1;
      that.Api_GET_EventsManagementCompetitionCertificationLevelQO.SkipCount = (that.Api_GET_EventsManagementCompetitionCertificationLevelQO.Page - 1) * that.Api_GET_EventsManagementCompetitionCertificationLevelQO.MaxResultCount;
      api_GET_EventsManagementCompetitionCertificationLevel(that.Api_GET_EventsManagementCompetitionCertificationLevelQO).then(res => {
        that.loading = false;
        that.gradeTable = res.items;
        that.totalPage = res.totalCount;
        that.getCompetitionCertificationLevel();
      }).catch(() => {
        that.loading = false;
      });
    },
    addGrade() {
      this.formData = {};
      this.$refs.addGrade.open();
      this.title = '新增认证等级';
      this.formData = this.SiSTKWEventsManagementCreateCompetitionCertificationLevelInput;
    },
    remove(row) {
      var that = this;
      that.$confirm(`您确定要删除 ${row.name}？`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api_DELETE_EventsManagementCompetitionCertificationLevelId(row.id).then(res => {
          that.getData(this.Api_GET_EventsManagementCompetitionCertificationLevelQO.Page);
          that.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          that.$message({
            type: 'error',
            message: '删除失败'
          });
        });
      }).catch(() => {
        that.$message({
          type: 'info',
          message: '取消删除'
        });
      });
    },
    edit(row) {
      this.formData = {};
      this.$refs.addGrade.open();
      CopyData(row, this.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput);
      this.title = '编辑认证等级';
      this.rowId = row.id;
      this.formData = this.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput;
    },
    locking(row) {
      var that = this;
      if (row.isLocked) {
        that.$confirm(`您确定要解锁 ${row.name}？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.isLocked = false;
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.name = row.name;
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.competitionLevelId = row.competitionLevelId;
          api_PUT_EventsManagementCompetitionCertificationLevelId(row.id, that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput).then(res => {
            that.getData(this.Api_GET_EventsManagementCompetitionCertificationLevelQO.Page);
            that.$message({
              type: 'success',
              message: '解锁成功!'
            });
          }).catch(() => {
            that.$message({
              type: 'error',
              message: '解锁失败'
            });
          });
        }).catch(() => {
          that.$message({
            type: 'info',
            message: '取消解锁'
          });
        });
      } else {
        that.$confirm(`您确定要锁定 ${row.name}？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.isLocked = true;
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.name = row.name;
          that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput.competitionLevelId = row.competitionLevelId;
          api_PUT_EventsManagementCompetitionCertificationLevelId(row.id, that.SiSTKWEventsManagementUpdateCompetitionCertificationLevelInput).then(res => {
            that.getData(this.Api_GET_EventsManagementCompetitionCertificationLevelQO.Page);
            that.$message({
              type: 'success',
              message: '锁定成功!'
            });
          }).catch(() => {
            that.$message({
              type: 'error',
              message: '锁定失败'
            });
          });
        }).catch(() => {
          that.$message({
            type: 'info',
            message: '取消锁定'
          });
        });
      }
    },
    // 分页
    sizeChange: function(MaxResultCount) {
      this.Api_GET_EventsManagementCompetitionCertificationLevelQO.MaxResultCount = MaxResultCount;
      this.getData();
    },
    pageIndexChange(currentPage) {
      this.getData(currentPage);
    }
  }
};
</script>

